//
//  AddressPickView.m
//  Learn
//
//  Created by zyl_macbookpro on 2024/4/12.
//  Copyright © 2024 迟建国. All rights reserved.
//

#import "AddressPickView.h"

@implementation AddressPickView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = rgba(0, 0, 0, 0.0);
        //self.picker
        self.hidden = YES;
        [self addSubview:self.alertView];
        [self.alertView setCorners:UIRectCornerTopLeft|UIRectCornerTopRight cornerRadius:15];
        [self.alertView addSubview:self.picker];
    }
    return self;
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    UITouch *t = [touches anyObject];
    CGPoint p = [t locationInView:self];
    if (p.y < self.alertView.y) {
        [self dismissView];
    }
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView;
{
    return 3;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    if (component == 0) {
        return self.provinceData.count;
    }else if (component == 1)
    {
        return self.cityDataShow.count;
    }
    return self.areaDataShow.count;
}
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component
{
    return lScreenWidth/3.00;
}
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
{
    return 52;
}

- (nullable NSAttributedString *)pickerView:(UIPickerView *)pickerView attributedTitleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    NSString *str = @"";
    if (component == 0) {
        NSDictionary *dic = self.provinceData[row];
        str =  dic[@"label"];
    }else if (component == 1)
    {
        NSDictionary *dic = self.cityDataShow[row];
        str =  dic[@"label"];
       
    }else{
        NSDictionary *dic = self.areaDataShow[row];
        str =  dic[@"label"];
        
    }
    
    NSMutableAttributedString *att = [[NSMutableAttributedString alloc] initWithString:str];
    [att addAttributes:@{NSForegroundColorAttributeName:rgba(51, 51, 51, 1),NSFontAttributeName:MFontWithSize(16)} range:att.string.rangeOfAll];
    return att;
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    if (component == 0) {
        self.provinceSelectedRow = row;
    }else if (component == 1)
    {
        self.cityDataSelectedRow = row;
    }else{
        self.areaDataSelectedRow = row;
    }
   [self dealData];
}
- (void)showView
{
    self.hidden = NO;
    [UIView animateWithDuration:0.4 animations:^{
        self.backgroundColor = rgba(0, 0, 0, 0.75);
        self.alertView.y = lScreenHeight - self.alertView.height;
    }];
}
- (void)dismissView
{
    [UIView animateWithDuration:0.2 animations:^{
        self.backgroundColor = rgba(0, 0, 0, 0.0);
        self.alertView.y = lScreenHeight;
    } completion:^(BOOL finished) {
        self.hidden = YES;
    }];
}
- (UIView *)alertView
{
    if (!_alertView) {
        _alertView = [[UIView alloc] initWithFrame:CGRectMake(0,lScreenHeight - 364,lScreenWidth,364)];
        _alertView.backgroundColor = lWhiteColor;
        [_alertView addSubview:separateLineWithColor(CGRectMake(0,57,lScreenWidth,0.5), rgba(229, 229, 229, 1))];
        UIButton *confirmBtn = ({
            UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
            button.frame = CGRectMake(lScreenWidth - 62, 0, 62, 57);
            [button setTitle:@"确定" forState:UIControlStateNormal];
            button.titleLabel.font = MFontWithSize(16.f);
            [button setTitleColor:rgba(12, 183, 108, 1) forState:UIControlStateNormal];
            [button addTarget:self action:@selector(confirmButtonClick) forControlEvents:UIControlEventTouchUpInside];
            button;
        });
        
        UIButton *cancelBtn = ({
            UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
            button.frame = CGRectMake(0, 0, 62, 57);
            [button setTitle:@"取消" forState:UIControlStateNormal];
            button.titleLabel.font = MFontWithSize(16.f);
            [button setTitleColor:rgba(142, 142, 142, 1) forState:UIControlStateNormal];
            [button addTarget:self action:@selector(cancelButtonClick) forControlEvents:UIControlEventTouchUpInside];
            button;
        });
        cancelBtn.backgroundColor = confirmBtn.backgroundColor = lClearColor;
        [_alertView addSubview:confirmBtn];
        [_alertView addSubview:cancelBtn];
    }
    return _alertView;
}
- (void)confirmButtonClick
{
    [self dismissView];
    if (self.block) {
        
        NSDictionary *dic1 = self.provinceData[self.provinceSelectedRow];
        NSString *str =  dic1[@"label"];
  
        NSDictionary *dic2 = self.cityDataShow[self.cityDataSelectedRow];
        NSString *str2 =  dic2[@"label"];
       

        NSDictionary *dic3 = self.areaDataShow[self.areaDataSelectedRow];
        NSString *str3 =  dic3[@"label"];
        
  
        NSString *address= [NSString stringWithFormat:@"%@/%@/%@",str,str2,str3];
        self.block(address);
    }
}
- (void)cancelButtonClick
{
    [self dismissView];
}
- (UIPickerView *)picker
{
    if (!_picker) {
        _picker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 57.5, lScreenWidth, 306)];
        _picker.delegate = self;
        _picker.dataSource = self;
        _picker.showsSelectionIndicator = YES;
        _picker.backgroundColor = lClearColor;
    }
    return _picker;
}
- (void)setData:(NSDictionary *)data
{
    _data = data;
    self.provinceData = self.data[@"provinceData"];
    self.cityData = self.data[@"cityData"];
    self.areaData = self.data[@"areaData"];
    [self dealData];
    if (self.block) {
        
        NSDictionary *dic1 = self.provinceData[self.provinceSelectedRow];
        NSString *str =  dic1[@"label"];
  
        NSDictionary *dic2 = self.cityDataShow[self.cityDataSelectedRow];
        NSString *str2 =  dic2[@"label"];
       

        NSDictionary *dic3 = self.areaDataShow[self.areaDataSelectedRow];
        NSString *str3 =  dic3[@"label"];
        
  
        NSString *address= [NSString stringWithFormat:@"%@/%@/%@",str,str2,str3];
        self.block(address);
    }
}
- (void)dealData
{
    if (self.cityData.count > self.provinceSelectedRow) {
        self.cityDataShow = self.cityData[self.provinceSelectedRow];
    }
    
    if (self.areaData.count > self.provinceSelectedRow) {
        NSArray *array = self.areaData[self.provinceSelectedRow];
       
        if (array.count <= self.cityDataSelectedRow) {
            self.cityDataSelectedRow = array.count - 1;
        }
        if (array.count > self.cityDataSelectedRow) {
            self.areaDataShow = array[self.cityDataSelectedRow];
        }
    }
    [self.picker reloadAllComponents];
}
@end
